﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.DALFactory;


namespace OgilvyOne.BLL
{
    public class CmsArticleBll
    {
        /// 查询文章
        /// </summary>
        /// <param name="caq"></param>
        /// <returns></returns>
        public IList getCmsArticleList(CmsArticleQuery caq)
        {
            return CmsArticleFactory.create().getCmsArticleList(caq);
        }
        /// <summary>
        /// 批量删除文章
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelCmsArticle(string ids)
        {
            if (CmsArticleFactory.create().Del(ids))
            {
                //--------需文章重新生成
                return CmsArticleFrontFactory.create().Del(ids);
            }
            return false;
        }
        /// <summary>
        /// 获取单篇文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticleModel(int aid)
        {
            return CmsArticleFactory.create().getCmsArticle(aid);
        }
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Add(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Insert(article);
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Update(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Update(article);
        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Pubone(int aid, string pubuname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "1", pubuname))
            {
                CmsArticleModel cm = CmsArticleFactory.create().getCmsArticle(aid);
                //-----需生成前台文章
                return CmsArticleFrontFactory.create().PushFront(cm);
            }
            return false;
        }
        /// <summary>
        /// 批量发布
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Pubs(string ids, string pubuname)
        {
            string[] idss = ids.Split(',');
            foreach (string id in idss)
            {
                if (id != null && id != "" && id != ",")
                {
                    Pubone(int.Parse(id), pubuname);
                }
            }
            return true;
        }
        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Audits(string ids, string state, string auditname)
        {
            string[] idss = ids.Split(',');
            foreach (string id in idss)
            {
                if (id != null && id != "" && id != ",")
                {
                    Audit(int.Parse(id), state, auditname);
                }
            }
            return true;
        }
        /// <summary>
        /// 审核文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Audit(int aid, string state, string auditname)
        {
            return CmsArticleFactory.create().AudutArticle(aid, state, auditname);
        }
        /// <summary>
        /// 撤消发布
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="state"></param>
        /// <param name="pubname"></param>
        /// <returns></returns>
        public bool RePubOne(int aid, string pubname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "6", pubname))
            {
                return CmsArticleFrontFactory.create().Del(aid.ToString());
            }
            return false;
        }

        /// <summary>
        /// 修改文章SEO
        /// </summary>
        /// <param name="seo"></param>
        /// <returns></returns>
        public bool UpdateSeo(CmsArticleModel seo)
        {
            if (CmsArticleFactory.create().UpdateSeo(seo))
            {
                return CmsArticleFactory.create().UpdateState(seo.Articleid, "0", seo.Modifyusername);
            }
            return false;
        }
        /// <summary>
        /// 获取文章SEO属性
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticelSeo(int aid)
        {
            return CmsArticleFactory.create().GetCmsArticelSeo(aid);
        }

        public bool UpdateAudit(int aid, string username)
        {
            return CmsArticleFactory.create().UpdateState(aid, "0", username);
        }
    }
}
